import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.Enumeration;

@WebServlet("/method")
public class Method extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html:charset=utf8");
        StringBuilder sb = new StringBuilder();
        //返回协议的名称和版本
        sb.append(req.getProtocol());
        sb.append("<br>");
        //返回方法名称:如get,post,put
        sb.append(req.getMethod());
        sb.append("<br>");
        //返回?前面的内容
        sb.append(req.getRequestURI());
        sb.append("<br>");
        //返回war包的名字
        sb.append(req.getContextPath());
        sb.append("<br>");
        //返回?后面的内容
        sb.append(req.getQueryString());
        sb.append("<br>");
        //获取在请求中包含参数的名称
        Enumeration<String> pNames = req.getParameterNames();
        while(pNames.hasMoreElements()){
            String name = pNames.nextElement();
            String value=req.getParameter(name);
            sb.append(name + "=" + value);
            sb.append("<br>");

        }

        //获取header
        //获取header里的key
        Enumeration<String> stringEnumeration = req.getHeaderNames();
        while (stringEnumeration.hasMoreElements()){
            String str = stringEnumeration.nextElement();
            //根据key获取header的value
            sb.append(str+":"+req.getHeader(str));
            sb.append("<br>");

        }
        //返回字符编码的名称
        sb.append(req.getCharacterEncoding());
        sb.append("<br>");
        //返回主题的contentType的类型
        sb.append(req.getContentType());
        sb.append("<br>");
        //返回body的长度
        sb.append(req.getContentLength());
        sb.append("<br>");
        resp.getWriter().write(sb.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-Type","text/html;charset=UTF-8");
        StringBuilder sb = new StringBuilder();
        //获取在请求中包含参数的名称
        Enumeration<String> pNames = req.getParameterNames();
        while(pNames.hasMoreElements()){
            String name = pNames.nextElement();
            String value=req.getParameter(name);
            sb.append(name + "=" + value);
            sb.append("<br>");

        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream inputStream = req.getInputStream();
        byte[]array = new byte[4096];
        int n = 0;
        int sum = 0;
        while ((n = inputStream.read(array,0,4096))!= -1){
            baos.write(array);
            sum+=n;
        }
        array = baos.toByteArray();
        String str = new String(array,0,sum);

        sb.append(str);
        resp.getWriter().write(sb.toString());
        System.out.println(sb.toString());

    }
}
